# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from pymongo import MongoClient


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""

"""

def get_rq(code):
    headers = {
        'Host': 'app2.jg.eastmoney.com',
        'Connection': 'keep-alive',
        'Accept': 'application/json',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36',
        'Referer': 'http://app2.jg.eastmoney.com/stock/cn/f9/collectMoneywin.html?securitycode=003031&date=2020-12-22&pagename=%E5%8B%9F%E9%9B%86%E8%B5%84%E9%87%91%E6%8A%95%E5%90%91',
        'Accept-Encoding': 'gzip, deflate',
    }
    url = f"http://app2.jg.eastmoney.com/stock/F9/getCollectCash?securityCode={code}&startDate=2019-01-01&endDate=2021-12-31&order=desc&reportPeriodType=1%2C5%2C3%2C6%2C7&exchangeRate=1&exchangeRateLabel=CNY&unitType=1"
    html = requests.get(url, headers=headers)

    data_json = json.loads(html.text[1:][:-1].replace('\\', ''))
    rq = data_json[6]['col0']
    print(rq)
    get_contents(code, rq)

def get_contents(code, rq):
    headers = {
        'Host': 'app2.jg.eastmoney.com',
        'Connection': 'keep-alive',
        'Accept': 'application/json',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36',
        'Referer': 'http://app2.jg.eastmoney.com/stock/cn/f9/collectMoneywin.html?securitycode=003031&date=2020-12-22&pagename=%E5%8B%9F%E9%9B%86%E8%B5%84%E9%87%91%E6%8A%95%E5%90%91',
        'Accept-Encoding': 'gzip, deflate',
    }

    url = f"http://app2.jg.eastmoney.com/stock/f9/CollectCashDetail?securityCode={code}&endDate={rq}"

    html = requests.get(url, headers=headers)

    data_json1 = json.loads(html.text[1:][:-1].replace('\\', ''))

    print(data_json1)


if __name__ == '__main__':
    get_rq("300122")
